package com.peng.sell.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.peng.sell.enums.ResponseCodeEnum;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 *  相应体
 * @param <T> 数据类型
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@NoArgsConstructor
public class ServerResponseVO<T> implements Serializable {
    private static final long serialVersionUID = 3648764175737035990L;
    private int code;
    private String msg;
    private T data;

    private ServerResponseVO(int code) {
        this.code = code;
    }

    private ServerResponseVO(int code, T data) {
        this.code = code;
        this.data = data;
    }

    private ServerResponseVO(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ServerResponseVO(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.code == ResponseCodeEnum.SUCCESS.getCode();
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }


    public static <T> ServerResponseVO<T> createBySuccess() {
        return new ServerResponseVO<T>(ResponseCodeEnum.SUCCESS.getCode());
    }

    public static <T> ServerResponseVO<T> createBySuccess(T data) {
        return new ServerResponseVO<T>(ResponseCodeEnum.SUCCESS.getCode(), data);
    }

    public static <T> ServerResponseVO<T> createBySuccess(String msg, T data) {
        return new ServerResponseVO<T>(ResponseCodeEnum.SUCCESS.getCode(), msg, data);
    }

    public static <T> ServerResponseVO<T> createBySuccessMsg(String msg) {
        return new ServerResponseVO<T>(ResponseCodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> ServerResponseVO<T> createByError() {
        return new ServerResponseVO<T>(ResponseCodeEnum.ERROR.getCode(), ResponseCodeEnum.ERROR.getDesc());
    }

    public static <T> ServerResponseVO<T> createByErrorMsg(String errMsg) {
        return new ServerResponseVO<T>(ResponseCodeEnum.ERROR.getCode(), errMsg);
    }

    public static <T> ServerResponseVO<T> createByError(int errCode, String errMsg) {
        return new ServerResponseVO<T>(errCode, errMsg);
    }

}
